<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddOrderIdToCardLogTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */


    public function up()
    {
        if (Schema::hasTable('yz_virtual_card_team_card_log')) {
            Schema::table('yz_virtual_card_team_card_log', function (Blueprint $table) {
                if (!Schema::hasColumn('yz_virtual_card_team_card_log', 'order_id')) {
                    $table->integer('order_id');
                }
                $member_ids = \DB::table('yz_virtual_card_team_reward_record')
                    ->select('id', 'member_id', 'order_id')
                    ->get()
                    ->toArray();

                foreach ($member_ids as $member_id) {
                    \DB::table('yz_virtual_card_team_card_log')
                        ->where('member_id', $member_id->member_id)
                        ->update(['order_id' => $member_id->order_id]);
                }
            });
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('yz_virtual_card_team_card_log');
    }
}
